/*
 * CharNGramEstLaplace.java
 *
 * Created on November 8, 2007, 10:16 AM
 *
 */
package Estimator.NGramChar;

/**
 *
 * @author Martin Haulrich
 */
public class CharNGramEstLaplace extends AbstractCharNGramEst {

    /**
     * Constructor.
     * Make new laplace estimator on the base of the ngrams in <code>ngrams</code>.
     * Only allow for estimates of <code>N</code>-grams. This should not be set higher than the N in <code>ngrams</code>.
     * @param ngrams N-grams that the estimate is based on.
     * @param N Estimates are made for up to N-grams.
     */
    public CharNGramEstLaplace(CharacterNGramCount ngrams, int N) {
        super(ngrams, N);
    }


    /**
     * Return the laplace-count for the given n-gram.
     * @param ngram ngram to get LaPlace count for.
     * @return Laplace-count for the given n-gram.
     */
    private double getLaplaceCount(String ngram) {
        int nl = ngram.length();
        int nlC = ngrams.getNCount(nl);
        //int ci1 = (double) ;
        //double nominator = (double) nlC;
        //  double denominator = (double) nlC + ngrams.getVocabSize(nl);
//        System.err.println("ngram:: " +ngram +"  "+ ngrams.getVocabSize(ngram.length()));
//        System.err.println("ci1 " + ci1 + "   nom: " + nominator + "  denom: " + denominator);
//        System.err.println("LapCpunt" + ci1*nominator/denominator);
        return (double) (ngrams.getCount(ngram) + 1) * nlC / (nlC + ngrams.getVocabSize(nl));

    }


    @Override
    public double getEstimate(String ngram) {
        if (ngram.length() > N) {
            System.err.println("Warning: this estimator only has up to " + N + "-grams.");
            System.err.println("Returning 0-probability " + ngram);
            return -2000;
        }
        return Math.log10(getLaplaceCount(ngram) / ((double) ngrams.getNCount(ngram.length())));
    }
}
